class CategoriesController < ApplicationController
  before_filter :check_administrator_role,:except => [:index, :show]
  #  protect_from_forgery :except => :update

  # GET /categories
  # GET /categories.xml
  before_filter :find_cart
  def index
    @categories = Category.paginate :page => params[:page],:per_page => 15

    respond_to do |format|
      format.html # index.html.erb
      format.xml  {render :xml => @categories.to_xml}
    end
  end


  # GET /categories/1
  # GET /categories/1.xml
#  def show
#    @category = Category.find(params[:id])
##    @styles = @category.elements.paginate :page => params[:page],
##      :per_page => 5 if @category.styles.length > 0
#    respond_to do |format|
#      format.html # show.html.erb
#      format.xml  { render :xml => @category }
#    end
#  end

  # GET /categories/new
  # GET /categories/new.xml
  def new
    @category = Category.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @category }
    end
  end

  # GET /categories/1/edit
  def edit
    @category = Category.find(params[:id])
  end

  # POST /categories
  # POST /categories.xml
  def create
    @category = Category.new(params[:category])

    respond_to do |format|
      if @category.save
        flash[:notice] = 'Category was successfully created.'
        format.html { redirect_to(@category) }
        format.xml  { render :xml => @category, :status => :created, :location => @category }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /categories/1
  # PUT /categories/1.xml
  def update
    @category = Category.find(params[:id])
    result = @category.update_attributes(params[:category])


    if !request.xhr?
      respond_to do |format|
        if result
          flash[:notice] = 'Category was successfully updated.'
          format.html { redirect_to(categories_url) }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @category.errors, :status => :unprocessable_entity }
        end
      end
    else
      if result
        render :text=> params[:category].values.to_s
      else
        render :text=>'false'
      end
    end
    
  end

  # DELETE /categories/1
  # DELETE /categories/1.xml
  def destroy
    @category = Category.find(params[:id])
    @category.destroy

    respond_to do |format|
      format.html { redirect_to(categories_url) }
      format.xml  { head :ok }
    end
  end

  def json_for_select
    json_arr = Array.new
    build_json(Category.find(:all,:conditions=>["parent_id is null"]),json_arr)
    return_json = json_arr.join(",")
    return_json = "{" + return_json + "}"
    render :text=>return_json
  end

  private

  def build_json(categories,json_arr)
    categories.each { |c|
      if c.children.size > 0
        json_arr << "'#{c.id}':'#{c.name_for_select}'"
        build_json(c.children,json_arr)
      else
        json_arr << "'#{c.id}':'#{c.name_for_select}'"
      end
    }
  end

  def build_tree(categories)
    data = Array.new
    categories.each { |category|
      if category.children.size > 0
        #        category.children_count = category.children.count
        #        category.save
        tmp_data = [{"text"=>category.name,"id"  => category.id,"cls"=>'file',
            "children" => build_tree(category.children) }]
        if data.empty?
          data =   tmp_data
        else
          data.concat(tmp_data)
        end

      else
        data.concat([{"text" => "<a href='/categories/#{category.id}/products'>#{category.name}(#{category.products.size})</a>","id" => category.id}])
      end

    }
    return data
  end
end
